﻿@{
    Layout = "~/Views/Shared/_Layout.cshtml";
    ViewBag.BodyClass = "easyui-layout";
}
<div id="tbtool" class="gou-toolbar" style="padding:3px;">
    <span>物料名称:</span>
    <input id="searchKey" name="searchKey" maxlength="25" style="line-height:26px;border:1px solid #ccc">
    <a href="javascript:;" class="easyui-linkbutton" iconcls="icon-search" onclick="Search();">查找</a>
    <a href="javascript:void(0);" onclick="access(0);" class="easyui-linkbutton" iconcls="icon-add">新增</a>
</div>
<table id="grid" data-options="region:'center',border:false" class="easyui-datagrid"
       rownumbers="true" fitcolumns="true" singleselect="true" fit="true" pagesize="50"></table>

@section Script{

    <script type="text/javascript">
        $(function () {
            function formatOper(val, row, index) {
                var rek = "";
                var cor = "";
                var opt = "";
                if (row.StopFlag) {
                    rek = "启用";
                    cor = "green";
                    opt = '<a href="javascript:void(0);" onclick="updateState(\'' + row.MaterialId + '\',0,\'' + row.UserName + '\');">启用</a>&nbsp|';
                }
                else {
                    rek = "禁用";
                    cor = "red";
                    opt = '<a href="javascript:void(0);" onclick="updateState(\'' + row.MaterialId + '\',1,\'' + row.UserName + '\');">禁用</a>&nbsp|';
                }
                opt = opt + '&nbsp<a href="javascript:void(0);" onclick="access(\'' + row.MaterialId + '\');">编辑</a>&nbsp|&nbsp;<a href="javascript:void(0);" onclick="deleteMaterial(\'' + row.MaterialId + '\');">删除</a>&nbsp';

                return opt ;
            }

            $('#grid').datagrid({
                url: 'GetList?name=' + $("#searchKey").val(),
                columns: [[
                     { field: 'MaterialId', title: 'Id', width: 70 }
                     ,{ field: 'MaterialName', title: '物料名', width: 150 }
                    , { field: 'CatagoryName', title: '分类', width: 120 }
                   
                    , { field: 'SingleWeight', title: '净重', width: 100 }
                    , {
                        field: 'IsStop', title: '状态', width: 50, formatter: function (val, row, index) {
                            var state = (row.IsStop) ? "禁用" : "启用";
                            return "<span>" + state + "</span>";
                        }
                    }
                    , { field: '_operate', title: '操作', width: 100, align: 'center', formatter: formatOper }
                ]],
                toolbar: '#tbtool',
                pagination: true
            });
            var pager = $('#grid').datagrid('getPager');	// get the pager of datagrid
            pager.pagination({
                showPageList: true,
                pageSize: 50
            });

        });
        function Search() {            
            $('#grid').datagrid("load", {
                name: $("#searchKey").val()
            });
        }

        function deleteMaterial(id) {
            var rek = "删除";
            $.messager.confirm(rek, "你确定" + rek + "”" + name + "“吗?", function (istrue) {
                if (istrue) {
                    var param = { materialId: id };
                    $.ajax({
                        "type": "Post",
                        "url": "ajaxDeleteMaterial?t=" + new Date().getTime(),
                        "dataType": "text",
                        "data": param,
                        "success": function (data) {
                            if (data == "True") {
                                $.messager.alert('提示', '删除成功!');
                                $("#grid").datagrid("reload");
                            }
                        }
                    });
                }
            });
           
        }

        function updateState(id, state,name) {
            var rek = state == 1 ? "禁用" : "启用";

            $.messager.confirm(rek, "你确定" + rek + "”" + name + "“吗?", function (istrue) {
                if (istrue) {
                    var param = { MaterialId: id };
                    $.ajax({
                        "type": "Post",
                        "url": "ajaxUpdateMaterialState?t=" + new Date().getTime(),
                        "dataType": "text",
                        "data": param,
                        "success": function (data) {
                            if (data == "True") {
                                $.messager.alert('提示', '操作成功!');
                                $("#grid").datagrid("reload");
                            }
                        }
                    });
                }
            });
        }


        function access(id) {
            window.location.href = "access?id=" + id;
        }
    </script>
}





